<template>
  <div class="cart-sku" ref="target">
    <div class="attrs" @click="toggle">
      <span class="ellipsis">{{ attrsText }}</span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="layer" v-if="visible">
      <GoodsSku @changeSku="changeSku" :skuId="skuId" :goods="goods"></GoodsSku>
      <XtxButton
        type="primary"
        size="mini"
        style="margin-left:60px"
        @click="submit"
      >
        确认
      </XtxButton>
    </div>
  </div>
</template>
<script>
import { ref } from 'vue'
import { onClickOutside } from '@vueuse/core'
import GoodsSku from '@/views/goods/components/goods-sku.vue'
import { getSpecsAndSkus } from '@/api/cart'
import { Message } from '@/components'
export default {
  name: 'CartSku',
  props: {
    attrsText: String,
    skuId: {
      type: String,
      default: ''
    }
  },
  components: {
    GoodsSku
  },
  setup(props, { emit }) {
    const visible = ref(false)
    const toggle = () => {
      visible.value = !visible.value
    }

    const target = ref(null)
    onClickOutside(target, () => {
      visible.value = false
    })

    // 发送请求-获取到商品的信息
    const goods = ref({})
    getSpecsAndSkus(props.skuId).then(res => {
      // console.log(res)
      goods.value = res.result
    })

    // 修改sku
    const currSku = ref({})
    const changeSku = sku => {
      currSku.value = sku
      console.log(currSku.value)
    }

    const submit = () => {
      // 传递给父组件的条件  1. 选择的规格完整  2. 渲染的规格发生了改变
      if (currSku.value.id && currSku.value.id !== props.skuId) {
        emit('change', currSku.value)
        visible.value = false
      } else {
        Message({ type: 'warning', text: '请选择或者修改商品规格' })
      }
    }
    return {
      visible,
      toggle,
      target,
      goods,
      changeSku,
      submit
    }
  }
}
</script>
<style scoped lang="less">
.cart-sku {
  height: 28px;
  border: 1px solid #f5f5f5;
  padding: 0 6px;
  position: relative;
  margin-top: 10px;
  display: inline-block;
  .attrs {
    line-height: 24px;
    display: flex;
    span {
      max-width: 230px;
      font-size: 14px;
      color: #999;
    }
    i {
      margin-left: 5px;
      font-size: 14px;
    }
  }
  .layer {
    position: absolute;
    left: -1px;
    top: 40px;
    z-index: 10;
    width: 400px;
    border: 1px solid @xtxColor;
    box-shadow: 2px 2px 4px lighten(@xtxColor, 50%);
    background: #fff;
    border-radius: 4px;
    font-size: 14px;
    padding: 20px;
    &::before {
      content: '';
      width: 12px;
      height: 12px;
      border-left: 1px solid @xtxColor;
      border-top: 1px solid @xtxColor;
      position: absolute;
      left: 12px;
      top: -8px;
      background: #fff;
      transform: scale(0.8, 1) rotate(45deg);
    }
  }
}
</style>
